<?php


namespace app\finance\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class HonourRecord extends BaseController
{
    protected $title='承兑登记单';
    protected $table='honour_record';
    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['hr.status','>',-1];

            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['hr.status','=',$this->param['status']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name|py.code|ad.name|hr.orderNum','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $where[]=['hr.orderDate','between',$dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('hr')
                ->leftJoin('purveyor py','py.id=hr.purveyorId')
                ->leftJoin('admin ad','ad.id=hr.checkId')
                ->where($where)
                ->field('hr.*,py.name as pName,ad.name as honourName')
                ->order('hr.id DESC')
                ->paginate($rows)->each(function ($item){
                    $item['crateTime']=date('Y-m-d',$item['create_time']);
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

    public function add()
    {
        $param=$this->param;
        $id=$param['id']??0;
        if ($this->request->post()){
            if (!$param['purveyorId']){
                return  to_assign(1,'参数缺失');
            }

            $param['update_time'] = time();

            try {
                if ($id){
                    add_log('edit', $id, $this->param,$this->title);
                    Db::name($this->table)->save($param);
                }else{
                    unset($param['id']);
                    $param['createId']=$this->uid;
                    $param['orderNum']=dJBHBySql($this->table,'orderNum','CDH'.date('Ymd'),false,1000);
                    $param['departId']=$this->did;
                    $param['create_time'] = time();
                    $id=Db::name($this->table)->insertGetId($param);
                    add_log('add', $id, $this->param,$this->title);
                }

            }catch (\Exception $e){
                return to_assign(1,'操作失败：'.$e->getMessage());
            }
            return  to_assign();

        }else{
            $punList=Db::name('purveyor')->where('status',1)->where('dw_type',2)->field('id,name')->select();

            View::assign('punList',$punList);
            View::assign('id',$id);
            return view('form');
        }
    }

    public function delete()
    {
        $ids=$this->param['id']??[];
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$ids)->column('status');
        if (in_array(2,$status)){
            return to_assign(1,'参数有误');
        }
        Db::startTrans();
        try {
            Db::name($this->table)->where('id','in',$ids)->update(['status'=>-1,'deleted'=>time()]);
            add_log('delete', $ids, $this->param,$this->title);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }

        return to_assign();
    }

    public function onUse()
    {
        $id=$this->param['id']??0;
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$id)->column('status');
        if (in_array(2,$status)||in_array(3,$status)){
            return to_assign(1,'参数有误');
        }

        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'checkId'=>$this->uid,
                'checkTime'=>date('Y-m-d H:i:s'),
                'registerTime'=>date('Y-m-d'),
                'status'=>2,
                'update_time'=>time()]);

            add_log('onUse', $id, $this->param,$this->title);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign(0,'操作成功');
    }

    public function onBack()
    {
        $id=$this->param['id']??0;
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$id)->column('status');
        if (in_array(1,$status)||in_array(3,$status)){
            return to_assign(1,'参数有误');
        }

        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'checkId'=>0,
                'checkTime'=>null,
                'registerTime'=>null,
                'status'=>1,
                'update_time'=>time()]);

            add_log('back', $id, $this->param,$this->title);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign(0,'操作成功');

    }
}